<template>
  <div class="search_container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>违章信息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="always">
      <!-- 搜索内容和导出区域 -->
      <el-row :gutter="10">
        <el-col :span="6" class="search-condition"
        ><span class="search-label">搜索条件:</span>
          <el-select
              v-model="queryInfo.condition"
              filterable
              clearable
              placeholder="请选择"
              style="margin-left: 0px"
          >
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="8" class="search-input">
          <span class="search-label">搜索:</span>
          <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              class="input-with-select"
              @keyup.enter.native="searchViolationByPage"
              clearable
          >
            <el-button
                slot="append"
                icon="el-icon-search"
                @click="searchViolationByPage"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2" style="float: right">
          <download-excel
              class="export-excel-wrapper"
              :data="tableData"
              :fields="json_fields"
              :header="title"
              name="图书违章表格.xls"
          >
            <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
            <el-button type="primary" class="el-icon-printer" size="mini"
            >导出Excel
            </el-button
            >
          </download-excel>
        </el-col>
        <el-col :span="2" style="float: right">
          <el-button
              type="primary"
              class="el-icon-printer"
              size="mini"
              @click="downLoad"
          >导出PDF
          </el-button
          >
        </el-col>
        <el-col :span="2" style="float: right">

          <el-button type="success" class="el-icon-full-screen" size="mini" @click="fullScreen"
          >全屏
          </el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="tableData" border height="550px" style="width: 100%" stripe id="pdfDom"
                :default-sort="{prop: 'cardNumber', order: 'ascending'}"
                v-loading="loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)">
        <el-table-column prop="violationId" label="序号" type="index" align="center"></el-table-column>
        <el-table-column prop="cardNumber" label="借阅证号" sortable align="center"></el-table-column>
        <el-table-column prop="bookNumber" label="图书编号" sortable align="center"></el-table-column>
        <el-table-column prop="borrowDate" label="借阅日期" sortable align="center"></el-table-column>
        <el-table-column prop="closeDate" label="截止日期" sortable align="center"></el-table-column>
        <el-table-column prop="returnDate" label="归还日期" sortable align="center"></el-table-column>
        <el-table-column prop="violationMessage" label="违章信息" align="center">
        </el-table-column>
        <el-table-column prop="violationAdmin" label="处理人" align="center">
        </el-table-column>
      </el-table>
      <!-- 分页查询区域 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.queryInfo.pageNum"
          :page-sizes="[10, 15, 20, 25, 30]"
          :page-size="this.queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
          style="margin-top: 20px; text-align: right;"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "book_number",
          label: "图书编号",
        },
        {
          value: "borrow_date",
          label: "借阅日期",
        },
        {
          value: "close_date",
          label: "截止日期",
        },
        {
          value: "return_date",
          label: "归还日期",
        },
        {
          value: "violation_message",
          label: "违章信息",
        },
      ],
      tableData: [
        {
          cardNumber: Number,
          bookNumber: Number,
          borrowDate: "",
          closeDate: "",
          returnDate: "",
          violationMessage: "",
          violationAdmin: "",
        },
      ],
      queryInfo: {
        pageNum: 1,
        pageSize: 5,
        condition: "",
        query: "",
        cardNumber: window.sessionStorage.getItem("cardNumber"),
      },
      total: 0,
      title: "图书违章表格",
      json_fields: {
        借阅证号: "cardNumber",
        图书编号: "bookNumber",
        借阅日期: "borrowDate",
        截止日期: "closeDate",
        归还日期: "returnDate",
        违章信息: "violationMessage",
        处理人: "violationAdmin"
      },
      loading: true
    };
  },
  methods: {
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;

      this.searchViolationByPage();
    },
    handleCurrentChange(val) {
      this.queryInfo.pageNum = val;

      this.searchViolationByPage();
    },
    async searchViolationByPage() {
      this.loading = true;
      const {data: res} = await this.$http.post(
          "user/get_violation",
          this.queryInfo
      );

      this.tableData = [];
      // console.log(res);
      if (res.status !== 200) {
        this.total = 0;
        this.loading = false;
        return this.$message.error(res.msg);
      }
      this.$message.success({
        message: res.msg,
        duration: 1000,
      });
      this.tableData = res.data.records;
      this.total = parseInt(res.data.total);
      this.loading = false;
    },
    downLoad() {
      this.getPdf(this.title); //参数是下载的pdf文件名
    },
    fullScreen() {
      // Dom对象的一个属性: 可以用来判断当前是否为全屏模式(trueORfalse)
      let full = document.fullscreenElement;
      // 切换为全屏模式
      if (!full) {
        // 文档根节点的方法requestFullscreen实现全屏模式
        document.documentElement.requestFullscreen();
      } else {
        // 退出全屏模式
        document.exitFullscreen();
      }
    }
  },
  created() {
    this.searchViolationByPage();
  },
};
</script>

<style scoped>
.search-condition, .search-input {
  display: flex;
  align-items: center;
}

.search-condition .search-label {
  font-size: 14px;
  width: 90px;
  font-weight: 600;
  color: #333;
}

.search-input .search-label {
  font-size: 14px;
  width: 50px;
  font-weight: 600;
  color: #333;
}
</style>